package com.yimi.yinhepda.views.moveStock_module.unload;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.RecycleAdapter.MoveStockManualAddAdapter;
import com.yimi.yinhepda.db.dao.MoveStockScanTableDao;
import com.yimi.yinhepda.entity.bean.ManualAddEntity;
import com.yimi.yinhepda.event.CountEvent;
import com.pda.ym.utils.SoundVibratorManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: lj
 * Date: 2018/12/25
 * Description: 移库卸车手动添加界面
 */
public class MoveStockUnloadManualAddActivity extends BaseActivity {

    /**
     * 运单号
     */
    private EditText etWaybill;
    /**
     * 撤销的数据列表
     */
    private RecyclerView rvdataList;
    private TextView tvTitle;
    public static Handler mHandler;
    private MoveStockManualAddAdapter mAddAdapter;
    private List<ManualAddEntity> addEntityList;
    private String mTaskNo;
    private TextView tvNum;
    private TextView tvAddCount;

    public MoveStockUnloadManualAddActivity() {
        super(true);
    }

    @Override
    public String getPageName() {
        return "移库卸车手动添加界面";
    }

    @SuppressLint("HandlerLeak")
    @Override
    protected void onStart() {
        super.onStart();
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MoveStockUnloadScanActivity.MOVE_STOCK_MANUAL:
                        Bundle data = msg.getData();
                        String result = data.getString("result");
                        loadData(result);
                        break;
                    default:
                        break;
                }
            }
        };
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move_stock_unload_manual_add);

        tvAddCount = findViewById(R.id.tv_addCount);
        tvNum = findViewById(R.id.num);
        tvTitle = findViewById(R.id.title);
        etWaybill = findViewById(R.id.et_waybill);
        findViewById(R.id.btn_add).setOnClickListener(this);
        rvdataList = findViewById(R.id.rv_add_dataList);
        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_sure).setOnClickListener(this);

        tvTitle.setText("手动添加");

        mTaskNo = getIntent().getStringExtra("taskNo");

        addEntityList = new ArrayList<>(0);
        mAddAdapter = new MoveStockManualAddAdapter(this, addEntityList);
        rvdataList.setLayoutManager(new LinearLayoutManager(this));
        rvdataList.setAdapter(mAddAdapter);

        //注册事件
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册事件
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_add:
                //添加按钮
                String waybillNo = etWaybill.getText().toString().trim();
                if ("".equals(waybillNo)) {
                    showToast("请先输入运单号");
                    return;
                }
                doscan(waybillNo);
                break;
            case R.id.iv_back:
                //返回按钮
                finish();
                break;
            case R.id.iv_sure:
                //确定按钮
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected void handleBarCode(String result) {
        if (result == null) {
            SoundVibratorManager.playSound(2);
            showToast("运单号扫描失败");
        } else {
            etWaybill.setText(result);
            doscan(result);
        }
    }

    private void doscan(String result) {
        //发送消息扫描运单
        Message msg = new Message();
        Bundle data = new Bundle();
        data.putString("result", result);
        msg.setData(data);
        msg.what = MoveStockUnloadScanActivity.MOVE_STOCK_MANUAL;
        MoveStockUnloadScanActivity.mHandler.sendMessage(msg);
    }

    private void loadData(String result) {
        boolean isExist = false;
        for (ManualAddEntity item : addEntityList) {
            if (item.getWaybillNo().equals(result)) {
                addEntityList.remove(item);
                addEntityList.add(0, item);
                isExist = true;
                break;
            }
        }
        if (!isExist) {
            ManualAddEntity addEntity = new ManualAddEntity();
            addEntity.setWaybillNo(result.substring(0, 12));
            addEntity.setChildWaybillNo(result);
            addEntityList.add(0, addEntity);
        }

        mAddAdapter.notifyDataSetChanged();
        excCount();
    }

    /**
     * 计算添加的票件
     */
    private void excCount() {
        List<String> operType = new ArrayList<>(0);
        operType.add("2");
        long unUploadCount = new MoveStockScanTableDao().getUnUploadCount(mTaskNo, operType);
        tvNum.setText(unUploadCount + "");

        Set<String> set = new HashSet<String>();
        for (ManualAddEntity item : addEntityList) {
            set.add(item.getWaybillNo());
        }
        tvAddCount.setText(set.size() + "/" + addEntityList.size());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(CountEvent countEvent) {
        tvNum.setText(countEvent.getCount() + "");
    }
}
